package database;

import java.sql.SQLException;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper;
import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.support.ConnectionSource;
import com.j256.ormlite.table.TableUtils;
import com.mymoney.R;

import entity.Account;
import entity.Category;
import entity.Plan;
import entity.Reference;
import entity.Report;
import entity.Tip;
import entity.Transaction;

/**
 * Database helper class used to manage the creation and upgrading of your database. This class also usually provides
 * the DAOs used by the other classes.
 */
public class DatabaseHelper extends OrmLiteSqliteOpenHelper {

	// name of the database file for your application -- change to something appropriate for your app
	private static final String DATABASE_NAME = "MyMoney.db";
	// any time you make changes to your database objects, you may have to increase the database version
	private static final int DATABASE_VERSION = 1;

	// the DAO object we use to access the tables
	private Dao<Account, Integer> accountDAO = null;

	private Dao<Category, Integer> categoryDAO = null;
	
	private Dao<Plan, Integer> planDAO = null;
	
	private Dao<Reference, Integer> referenceDAO = null;
	
	private Dao<Report, Integer> reportDAO = null;
	
	private Dao<Tip, Integer> tipDAO = null;
	
	private Dao<Transaction, Integer> transactionDAO = null;
	
	public DatabaseHelper(Context context) {
		super(context, DATABASE_NAME, null, DATABASE_VERSION, R.raw.ormlite_config);
	}

	/**
	 * This is called when the database is first created. Usually you should call createTable statements here to create
	 * the tables that will store your data.
	 */
	@Override
	public void onCreate(SQLiteDatabase db, ConnectionSource connectionSource) {
		try {
			Log.i(DatabaseHelper.class.getName(), "onCreate");
			
			TableUtils.createTable(connectionSource, Account.class);
			TableUtils.createTable(connectionSource, Category.class);
			TableUtils.createTable(connectionSource, Plan.class);
			TableUtils.createTable(connectionSource, Reference.class);
			TableUtils.createTable(connectionSource, Report.class);
			TableUtils.createTable(connectionSource, Tip.class);
			TableUtils.createTable(connectionSource, Transaction.class);
			
			Log.i(DatabaseHelper.class.getName(), "create database successfully");
		} catch (SQLException e) {
			Log.e(DatabaseHelper.class.getName(), "Can't create database", e);
			throw new RuntimeException(e);
		}

//		// here we try inserting data in the on-create as a test
//		Dao<SimpleData, Integer> dao = getSimpleDataDao();
//		long millis = System.currentTimeMillis();
//		// create some entries in the onCreate
//		SimpleData simple = new SimpleData(millis);
//		dao.create(simple);
//		simple = new SimpleData(millis + 1);
//		dao.create(simple);
//		Log.i(DatabaseHelper.class.getName(), "created new entries in onCreate: " + millis);
	}

	/**
	 * This is called when your application is upgraded and it has a higher version number. This allows you to adjust
	 * the various data to match the new version number.
	 */
	@Override
	public void onUpgrade(SQLiteDatabase db, ConnectionSource connectionSource, int oldVersion, int newVersion) {
		try {
			Log.i(DatabaseHelper.class.getName(), "onUpgrade");
			TableUtils.dropTable(connectionSource, Account.class, true);
			TableUtils.dropTable(connectionSource, Category.class, true);
			TableUtils.dropTable(connectionSource, Plan.class, true);
			TableUtils.dropTable(connectionSource, Reference.class, true);
			TableUtils.dropTable(connectionSource, Report.class, true);
			TableUtils.dropTable(connectionSource, Tip.class, true);
			TableUtils.dropTable(connectionSource, Transaction.class, true);
			// after we drop the old databases, we create the new ones
			onCreate(db, connectionSource);
		} catch (SQLException e) {
			Log.e(DatabaseHelper.class.getName(), "Can't drop databases", e);
			throw new RuntimeException(e);
		}
	}

	/**
	 * Returns the Database Access Object (DAO) for our SimpleData class. It will create it or just give the cached
	 * value.
	 */
//	public Dao<SimpleData, Integer> getDao() throws SQLException {
//		if (simpleDao == null) {
//			simpleDao = getDao(SimpleData.class);
//		}
//		return simpleDao;
//	}

	/**
	 * Returns the Dao (Database Access Object) version of a Dao for our SimpleData class. It will
	 * create it or just give the cached value. Dao only through s.
	 * @throws SQLException 
	 */
	public Dao<Account, Integer> getAccountDao() throws SQLException {
		if (accountDAO == null) {
			accountDAO = getDao(Account.class);
		}
		return accountDAO;
	}
	
	public Dao<Category, Integer> getCategoryDao() throws SQLException {
		if (categoryDAO == null) {
			categoryDAO = getDao(Category.class);
		}
		return categoryDAO;
	}
	
	public Dao<Plan, Integer> getPlanDao() throws SQLException {
		if (planDAO == null) {
			planDAO = getDao(Plan.class);
		}
		return planDAO;
	}
	
	public Dao<Reference, Integer> getReferenceDao() throws SQLException {
		if (referenceDAO == null) {
			referenceDAO = getDao(Reference.class);
		}
		return referenceDAO;
	}
	
	public Dao<Report, Integer> getReportDao() throws SQLException {
		if (reportDAO == null) {
			reportDAO = getDao(Report.class);
		}
		return reportDAO;
	}
	
	public Dao<Tip, Integer> getTipDao() throws SQLException {
		if (tipDAO == null) {
			tipDAO = getDao(Tip.class);
		}
		return tipDAO;
	}
	
	public Dao<Transaction, Integer> getTransactionDao() throws SQLException {
		if (transactionDAO == null) {
			transactionDAO = getDao(Transaction.class);
		}
		return transactionDAO;
	}
	
	/**
	 * Close the database connections and clear any cached DAOs.
	 */
	@Override
	public void close() {
		super.close();
//		simpleDao = null;
	}
}
